//Copyright (C) 2008 QMTech (http://www.qmtech.net)

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections;
using System.Reflection;

using MonoDevelop.Projects.Dom;
using MonoDevelop.Projects.Gui.Completion;

namespace MonoDevelop.Castle.Monorail
{
	
	
	public class NVelocityMethodParameterDataProvider : IParameterDataProvider
	{

		MethodInfo[] listMethods;
		string methodName;
		
		public NVelocityMethodParameterDataProvider(Type type, string methodName)
		{
			this.methodName = methodName;
			ArrayList methods = new ArrayList ();
			foreach (MethodInfo mi in type.GetMethods()) {
				if (mi.Name == methodName) 
					methods.Add (mi);
			}
			Init (methods);
		}

		void Init (ArrayList methods)
		{
			listMethods = new MethodInfo [methods.Count];
			methods.CopyTo (listMethods, 0);
			int[] parCount = new int [methods.Count];
			for (int i=0; i<methods.Count; i++)
				parCount[i] = listMethods [i].GetParameters().Length;
			Array.Sort (parCount, listMethods, 0, parCount.Length);
		}

		public int OverloadCount { 
			get { return listMethods.Length; } 
		}
		
		public int GetCurrentParameterIndex (ICodeCompletionContext ctx){return 0;}
		
		public string GetMethodMarkup (int overload, string[] parameterMarkup){
			return methodName + " (" + string.Join (", ", parameterMarkup)  + ")";
		}
		
		public string GetParameterMarkup (int overload, int paramIndex){
			return GetParameter (overload, paramIndex).Name;
		}
		
		public int GetParameterCount (int overload){
			return GetMethod (overload).GetParameters().Length;
		}

		protected MethodInfo GetMethod (int overload)
		{
			return (MethodInfo) listMethods [overload];
		}
		
		protected ParameterInfo GetParameter (int overload, int paramIndex)
		{
			return GetMethod (overload).GetParameters()[paramIndex];
		}
	}
}
